package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.FriendMovement;
import com.itheima.domain.mongo.MyMovement;
import com.itheima.service.mongo.FriendService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class FriendServiceImpl implements FriendService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //添加好友
    @Override
    public void addContacts(Long userId, Long friendId) {
        //判断我好友列表是否曾经有他
        boolean exists1 = mongoTemplate.exists(new Query(Criteria.where("userId").is(userId).and("friendId").is(friendId)), Friend.class);

        if (exists1 == false) {
            Friend friend1 = new Friend();
            friend1.setCreated(System.currentTimeMillis());
            friend1.setUserId(userId);
            friend1.setFriendId(friendId);
            mongoTemplate.save(friend1);

            // 1-3 查询他（好友）的个人动态  movement_mine_12
            List<MyMovement> friendMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + friend1);
            if (CollectionUtil.isNotEmpty(friendMovements)) {
                for (MyMovement friendMovement : friendMovements) {
                    FriendMovement myfriendMovement = new FriendMovement();
                    myfriendMovement.setPublishId(friendMovement.getPublishId());
                    myfriendMovement.setUserId(friendId);
                    myfriendMovement.setCreated(friendMovement.getCreated());
                    mongoTemplate.save(myfriendMovement, ConstantUtil.MOVEMENT_FRIEND + userId);
                }
            }
        }

        boolean exists2 = mongoTemplate.exists(new Query(Criteria.where("userId").is(friendId).and("friendId").is(userId)), Friend.class);
        if (exists2 == false) {
            Friend friend2 = new Friend();
            friend2.setCreated(System.currentTimeMillis());
            friend2.setFriendId(userId);
            friend2.setUserId(friendId);
            mongoTemplate.save(friend2);

            List<MyMovement> myMovements = mongoTemplate.findAll(MyMovement.class, ConstantUtil.MOVEMENT_MINE + userId);
            if (CollectionUtil.isNotEmpty(myMovements)) {
                for (MyMovement myMovement : myMovements) {

                    FriendMovement friendMovement = new FriendMovement();
                    friendMovement.setCreated(myMovement.getCreated());
                    friendMovement.setPublishId(myMovement.getPublishId());
                    friendMovement.setUserId(userId);
                    mongoTemplate.save(friendMovement, ConstantUtil.MOVEMENT_FRIEND + friendId);
                }
            }
        }
    }


    // 查看联系人
    @Override
    public PageBeanVo findContactByPage(Long userId, Integer pageNum, Integer pageSize) {

        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).skip((pageNum - 1) * pageSize).limit(pageSize);
        List<Friend> friends = mongoTemplate.find(query, Friend.class);
        long count = mongoTemplate.count(new Query(Criteria.where("userId").is(userId)), Friend.class);
        return new PageBeanVo(pageNum, pageSize, count, friends);
    }
}
